package com.injurydetection.www.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.injurydetection.www.R;
import com.injurydetection.www.bean.DataBean;
import com.injurydetection.www.bean.MainRvData;
import com.injurydetection.www.util.DateUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by sunguiyong on 2020/8/11
 */
public class MainRvAdapter extends RecyclerView.Adapter<MainRvAdapter.MyViewHolder> {
    //    private List<MainRvData> mMainRvDataList;
    private Context mContext;

    public void setDataBeans(List<DataBean> dataBeans) {
        mDataBeans = dataBeans;
    }

    private List<DataBean> mDataBeans;

//    public MainRvAdapter(List<MainRvData> mainRvDataList, Context context) {
//        mMainRvDataList = mainRvDataList;
//        mContext = context;
//    }

    public MainRvAdapter(List<DataBean> dataBean, Context context) {
        this.mDataBeans = dataBean;
        mContext = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_main_notes, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        long time1 = mDataBeans.get(position).getCreateTime().getTime();
        long time2 = new Date().getTime();
        holder.time.setText(DateUtils.getDistanceTimeMy(time2, time1));
        holder.name.setText(mDataBeans.get(position).getName());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getClickListener().onItemClick(mDataBeans, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataBeans == null ? 0 : (mDataBeans.size() > 10 ? 10 : mDataBeans.size());
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView time, name;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.time_tv);
            name = itemView.findViewById(R.id.name_tv);
        }
    }

    private ClickListener mClickListener;

    public ClickListener getClickListener() {
        return mClickListener;
    }

    public void setClickListener(ClickListener clickListener) {
        mClickListener = clickListener;
    }

    public interface ClickListener {
        void onItemClick(List<DataBean> dataBeans, int positon);
    }
}
